global data /projects/mortality/data/
global userdata /projects/mortality/data/
global output /projects/mortality/output/

global controls black white hispanic female i.age
global econcty unemp_rate medhhinc povrate
global opioid mustaccess pdmp painlaw medmj dispmj i.triplicate##i.dyear

global outcomes anymcaid days2 cumdays

***A) Prepare data for analysis

use "$data/mcaidenroll.dta", clear
gen dyear=enrollyr
destring dyear, replace
compress
save "$data/mcaidenroll.dta", replace

use $data/panelfinalelig, clear
drop if pik==""

merge m:1 dyear pik using $data/mcaidenroll, keep(1 3) nogen

replace dayselig=0 if dayselig==.
gen anymcaid=0
replace anymcaid=1 if dayselig > 0 & dayselig!=.
replace anymcaid=. if dyear==2017
replace dayselig=. if dyear==2017

bys dyear: summ anymcaid
bys st dyear: summ anymcaid
summ dayselig, d

save $data/mcaidfinal, replace

*use $data/mcaidfinal, clear

***IMPUTE 2017 ELIGIBILITY USING 2015-2016 TRANSITIONS
gen days2016=0
replace days2016=dayselig if dyear==2016
gen days2015=0
replace days2015=dayselig if dyear==2015
bys pik: egen days2016a=max(days2016)
bys pik: egen days2015a=max(days2015)

gen anymcaid2015=0
replace anymcaid2015=1 if days2015 > 0

gen daysgroup=.
replace daysgroup=0 if days2015a==0
replace daysgroup=1 if days2015a >0 & days2015a < 91
replace daysgroup=2 if days2015a > 90 & days2015a < 182
replace daysgroup=3 if days2015a > 181 & days2015a < 274
replace daysgroup=4 if days2015a > 273 

gen expstate=0
replace expstate=1 if treated14==1
replace expstate=1 if treated15==1
replace expstate=1 if treated16==1
replace expstate=1 if treated17==1

keep if dyear==2016

reg days2016a expstate i.daysgroup i.target1 i.target1##i.expstate i.expstate##i.daysgroup i.target1##i.daysgroup i.target1##i.daysgroup##i.expstate i.age2014 i.female i.blacknh i.whitenh i.hispanic
estimates save $data/predictcoefs, replace

use $data/mcaidfinal, clear


gen days2016=0
replace days2016=dayselig if dyear==2016
bys pik: egen days2016a=max(days2016)

gen daysgroup=.
replace daysgroup=0 if days2016a==0
replace daysgroup=1 if days2016a < 91 & days2016a > 0
replace daysgroup=2 if days2016a > 90 & days2016a < 182
replace daysgroup=3 if days2016a > 181 & days2016a < 274
replace daysgroup=4 if days2016a > 273 

gen expstate=0
replace expstate=1 if treated14==1
replace expstate=1 if treated15==1
replace expstate=1 if treated16==1
replace expstate=1 if treated17==1

*Old version:
*merge m:1 st daysgroup target1 using $data/imputed_days

estimates use $data/predictcoefs
predict days2017_p

summ days2017_p
replace days2017_p=0 if days2017_p==.
gen anymcaid2017_p=0
replace anymcaid2017_p=1 if days2017_p > 0

gen days2=dayselig
replace days2=days2017_p if dyear==2017
replace days2=. if st==22 & dyear==2015

gen anymcaid2=anymcaid
replace anymcaid2=anymcaid2017_p if dyear==2017
replace anymcaid2=. if st==22 & dyear==2015
replace anymcaid=. if st==22 & dyear==2015

bys pik (dyear): gen cumdays=sum(days2)

gen evermcaid=0
replace evermcaid=1 if cumdays > 0
replace evermcaid=. if deathyear < dyear

bys dyear: summ anymcaid2 days2 cumdays 

gen post=0
replace post=1 if dyear > 2013

bys pik post: egen evermcaidpp=max(anymcaid2)

summ evermcaidpp if post==1 & treated14==1
summ evermcaidpp if post==1 & expstate==0
summ evermcaidpp if post==0 & treated14==1
summ evermcaidpp if post==0 & expstate==0

gen anymcaidpre1=anymcaid if dyear < 2014
bys pik: egen anymcaidpre=max(anymcaidpre1)

replace cumdays=. if st==22 & dyear > 2014

save $data/mcaidfinalfinal, replace

*use $data/mcaidfinalfinal, clear

replace days2=. if dyear==2017
replace cumdays=. if st==22 & dyear==2015


foreach y in $outcomes {
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & age2014 > 54 & dyear < 2014 & expstate==1
scalar ymean=r(mean)
outreg2 using $output/firststage.xls, ctitle("Main Sample", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & age2014 > 54 & dyear < 2014 & expstate==1
scalar ymean=r(mean)
outreg2 using $output/firststage.xls, ctitle("Main Sample", "`y'") addstat("pre-exp mean", ymean) append 
}

gen target1b=0
replace target1b=1 if povpi < 139
replace target1b=1 if schl < 16
replace target1b=0 if age2014 < 55
replace target1b=0 if ssi > 0
replace target1b=0 if cit=="5"


gen target2=0
replace target2=1 if povpi < 139
replace target2=1 if schl < 16
replace target2=0 if ssi > 0
replace target2=0 if cit=="5"

***Other samples

foreach y in $outcomes {

**65+ in 2014

reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if age2014 > 64, absorb(st dyear year) cluster(st)
summ `y' if age2014 > 64 & dyear < 2014 & expstate==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("65+ in 2014", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if age2014 > 64, absorb(st dyear year) cluster(st)
summ `y' if age2014 > 64 & dyear < 2014 & expstate==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("65+ in 2014", "`y'") addstat("pre-exp mean", ymean) append 

**High Income
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if  povpi > 399 & age2014 > 54 & age2014 < 65 , absorb(st dyear year) cluster(st)
summ `y' if povpi > 399 & dyear < 2014 & expstate==1 & age2014 > 54 & age2014 < 65
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("400+FPL Age 55+", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if povpi > 399 & age2014 > 54 & age2014 < 65, absorb(st dyear year) cluster(st)
summ `y' if povpi > 399 & dyear < 2014 & expstate==1 & age2014 > 54 & age2014 < 65
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("400+FPL  Age 55+", "`y'") addstat("pre-exp mean", ymean) append 

**All Ages
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 18 & age2014 < 65, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("All Ages", "`y'") addstat("pre-exp mean", ymean) append 

reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & age2014 > 18 & age2014 < 65, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("All Ages", "`y'") addstat("pre-exp mean", ymean) append 

**Black NH
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & blacknh==1 , absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & blacknh==1 
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Black NH", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & blacknh==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & blacknh==1 
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Black NH", "`y'") addstat("pre-exp mean", ymean) append 

**White NH
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & whitenh==1 , absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & whitenh==1 
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("White NH", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1  & whitenh==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & whitenh==1 
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("White NH", "`y'") addstat("pre-exp mean", ymean) append 


**Other NH
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & whitenh==0 & blacknh==0 & hispanic==0, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & whitenh==0 & blacknh==0 & hispanic==0
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Other NH", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1  & whitenh==0 & blacknh==0 & hispanic==0 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & whitenh==0 & blacknh==0 & hispanic==0
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Other NH", "`y'") addstat("pre-exp mean", ymean) append 


**Hispanic
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & hispanic==1, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & hispanic==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Hispanic", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & hispanic==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & hispanic==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Hispanic", "`y'") addstat("pre-exp mean", ymean) append 


**Uninsured
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & noinsure==1, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & noinsure==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Uninsured", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & noinsure==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & noinsure==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Uninsured", "`y'") addstat("pre-exp mean", ymean) append 


**Women
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & female==1, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & female==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Women", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & female==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & female==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Women", "`y'") addstat("pre-exp mean", ymean) append 


**Men
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & female==0, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & female==0
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Men", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & female==0 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & female==0
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Men", "`y'") addstat("pre-exp mean", ymean) append 


**LT 138 FPL
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & povpi < 139, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & povpi < 139
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("LT 138", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & povpi < 139 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & povpi < 139
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("LT 138", "`y'") addstat("pre-exp mean", ymean) append 

**LT HS Education
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & lths==1, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & lths==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("LT HS", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & lths==1 & age2014 > 54, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & lths==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("LT HS", "`y'") addstat("pre-exp mean", ymean) append 

**Age 55-61
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & age2014 < 62, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & age2014 < 62
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Age 55-61", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & age2014 > 54 & age2014 < 62, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54 & age2014 < 62
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Age 55-61", "`y'") addstat("pre-exp mean", ymean) append 


summ age2014 if newlyelig==1

**Newly Eligible
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if age2014 > 54 & age2014 < 65 & newlyelig==1, absorb(st dyear year) cluster(st)
outreg2 using $output/shadac_fs.xls, ctitle("Newly Eligible", "`y'")  append 
reghdfe `y' posttreated  [pweight=pwgt] if age2014 > 54 & age2014 < 65 & newlyelig==1, absorb(st dyear year) cluster(st)
outreg2 using $output/shadac_fs.xls, ctitle("Newly Eligible", "`y'") append 

**Newly Eligible-In Target
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if age2014 > 54 & age2014 < 65 & newlyelig==1 & target1==1, absorb(st dyear year) cluster(st)
outreg2 using $output/shadac_fs.xls, ctitle("Newly Eligible Target", "`y'")  append 
reghdfe `y' posttreated  [pweight=pwgt] if age2014 > 54 & age2014 < 65 & newlyelig==1  & target1==1, absorb(st dyear year) cluster(st)
outreg2 using $output/shadac_fs.xls, ctitle("Newly Eligible Target", "`y'") append 

**SHADAC Income
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if newtarget==1 & age2014 > 54, absorb(st dyear year) cluster(st)
outreg2 using $output/shadac_fs.xls, ctitle("SHADAC Income", "`y'") append 
reghdfe `y' posttreated  [pweight=pwgt] if newtarget==1 & age2014 > 54, absorb(st dyear year) cluster(st)
outreg2 using $output/shadac_fs.xls, ctitle("SHADAC Income", "`y'") append 

**No Mcaid in Pre
reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if target1==1 & age2014 > 54 & anymcaidpre==0, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54  & anymcaidpre==0
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("No Mcaid Pre 2014", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if target1==1 & age2014 > 54  & anymcaidpre==0, absorb(st dyear year) cluster(st)
summ `y' if target1==1 & dyear < 2014 & expstate==1  & age2014 > 54  & anymcaidpre==0
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("No Mcaid Pre 2014", "`y'") addstat("pre-exp mean", ymean) append 


**Married

reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if msp=="1" & age2014 > 54 & target1==1, absorb(st dyear year) cluster(st)
summ `y' if msp=="1" & dyear < 2014 & expstate==1  & age2014 > 54 & target1==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Married", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if msp=="1" & age2014 > 54 & target1==1, absorb(st dyear year) cluster(st)
summ `y' if msp=="1" & dyear < 2014 & expstate==1  & age2014 > 54  & target1==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Married", "`y'") addstat("pre-exp mean", ymean) append 


**Not Married

reghdfe `y' eventm5 eventm4 eventm3 eventm2 eventm1 event1 event2 event3 event4 [pweight=pwgt] if msp!="1" & age2014 > 54 & target1==1, absorb(st dyear year) cluster(st)
summ `y' if msp!="1" & dyear < 2014 & expstate==1  & age2014 > 54 & target1==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Not Married", "`y'") addstat("pre-exp mean", ymean) append 
reghdfe `y' posttreated  [pweight=pwgt] if msp!="1" & age2014 > 54 & target1==1, absorb(st dyear year) cluster(st)
summ `y' if msp!="1" & dyear < 2014 & expstate==1  & age2014 > 54 & target1==1
scalar ymean=r(mean)
outreg2 using $output/firststage_het.xls, ctitle("Not Married", "`y'") addstat("pre-exp mean", ymean) append 

reghdfe `y' posttreated  [pweight=pwgt]  if target1==1 & age2014 > 18 & age2014 < 30, absorb(st dyear year) cluster(st)
outreg2 using $output/age_fs.xls, ctitle("Age 19 to 29", "`y'") append 

reghdfe `y' posttreated  [pweight=pwgt]  if target1==1 & age2014 > 29 & age2014 < 40, absorb(st dyear year) cluster(st)
outreg2 using $output/age_fs.xls, ctitle("Age 30-39", "`y'") append 

reghdfe `y' posttreated  [pweight=pwgt]  if target1==1 & age2014 > 39 & age2014 < 50, absorb(st dyear year) cluster(st)
outreg2 using $output/age_fs.xls, ctitle("Age 40-49", "`y'") append 

reghdfe `y' posttreated  [pweight=pwgt]  if target1==1 & age2014 > 49 & age2014 < 56, absorb(st dyear year) cluster(st)
outreg2 using $output/age_fs.xls, ctitle("Age 50-54", "`y'") append 

}


